package com.lovo.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/qd/image")
public class UploadController {

    @PostMapping("uploadImg")
    public String uploadImg(@RequestParam("fileName")MultipartFile file) throws IOException {

        String fileName = file.getOriginalFilename();
        //重命名
        fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));

        //格式验证
        String [] files = fileName.split("[.]");
        if (!(files[1].equals("jpg")||files[1].equals("png"))){
            return "no";
        }

        //判断文件夹是否存在
        File file1 = new File("D:/JAVA/img/"+fileName);
        if (!file1.getParentFile().exists()) {
            file1.getParentFile().mkdirs();
        }

        //拷贝
        file.transferTo(file1);

        return fileName;
    }
}
